package com.youpai.platform.chat.service.converter;

import com.youpai.platform.chat.service.entity.dto.ChatMessageDTO;
import com.youpai.platform.chat.service.entity.po.ChatMessagePO;
import com.youpai.platform.chat.service.util.ChatUtils;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @date: 2022/7/22 12:21
 * @author: Anna
 * @version: 1.0
 * @description: 聊天消息转化类
 */
public class ChatMessageConverter {

    /**
     * ChatMessagePO列表转化成ChatMessageDTO列表
     *
     * @param chatMessagePOList chatMessagePOList列表
     * @return chatMessageDTO 列表
     */
    public static LinkedList<ChatMessageDTO> convertChatMessagePOList2ChatMessageDTOList(List<ChatMessagePO> chatMessagePOList) {
        if (CollectionUtils.isEmpty(chatMessagePOList)) {
            return new LinkedList<>();
        }
        LinkedList<ChatMessageDTO> chatMessageDTOList = new LinkedList<>();
        Long preMsgId = 0L;
        for (ChatMessagePO chatMessagePO : chatMessagePOList) {
            ChatMessageDTO chatMessageDTO = ChatMessageConverter.convertChatMessagePO2ChatMessageDTO(chatMessagePO);
            chatMessageDTO.setPreMsgId(preMsgId);
            chatMessageDTO.setToAccId(chatMessagePO.getToAccId());
            chatMessageDTO.setToUid(ChatUtils.getUidByAccId(chatMessagePO.getToAccId()));
            preMsgId = chatMessageDTO.getMsgId();
            chatMessageDTOList.add(chatMessageDTO);
        }
        return chatMessageDTOList;
    }

    /**
     * ChatMessagePO转化成ChatMessageDTO
     *
     * @param chatMessagePO ChatMessagePO
     * @return ChatMessageDTO
     */
    public static ChatMessageDTO convertChatMessagePO2ChatMessageDTO(ChatMessagePO chatMessagePO) {
        if (null == chatMessagePO) {
            return new ChatMessageDTO();
        }
        ChatMessageDTO chatMessageDTO = new ChatMessageDTO();
        chatMessageDTO.setContent(chatMessagePO.getContent())
                .setMsgId(chatMessagePO.getMessageId())
                .setFromAccId(chatMessagePO.getFromAccId())
                .setFromUid(ChatUtils.getUidByAccId(chatMessagePO.getFromAccId()))
                .setTimeStamp(chatMessagePO.getTimeStamp())
                .setMsgType(chatMessagePO.getMsgType())
                .setSessionId(chatMessagePO.getSessionId());
        return chatMessageDTO;
    }
}
